package com.hdy.common.loadimage;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.File;

public class LoadImageController {

    private static LoadImageController sLoadImageController;
    private Context mContext;

    public static LoadImageController getInstance() {
        if (sLoadImageController == null) {
            synchronized (LoadImageController.class) {
                if (sLoadImageController == null) {
                    sLoadImageController = new LoadImageController();
                }
            }
        }
        return sLoadImageController;
    }

    public void init(Context context) {
        mContext = context;
    }

    /**
     * 加载网络图片
     * @param url
     * @param imageView
     */
    public void loadImage(String url, ImageView imageView) {
        Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载资源图片
     * @param resId
     * @param imageView
     */
    public void loadImage(int resId, ImageView imageView) {
        Glide.with(mContext).load(resId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载本地图片
     * @param file
     * @param imageView
     */
    public void loadImage(File file, ImageView imageView) {
        Glide.with(mContext).load(file).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载uri图片
     * @param uri
     * @param imageView
     */
    public void loadImage(Uri uri, ImageView imageView) {
        Glide.with(mContext).load(uri).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param url
     * @param imageView
     */
    public void loadBitmap(String url, ImageView imageView) {
        Glide.with(mContext).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param file
     * @param imageView
     */
    public void loadBitmap(File file, ImageView imageView) {
        Glide.with(mContext).load(file).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param resId
     * @param imageView
     */
    public void loadBitmap(int resId, ImageView imageView) {
        Glide.with(mContext).load(resId).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param url
     * @param placeHolderId
     * @param imageView
     */
    public void loadBitmap(String url, int placeHolderId,ImageView imageView) {
        Glide.with(mContext).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param resId
     * @param placeHolderId
     * @param imageView
     */
    public void loadBitmap(int resId, int placeHolderId, ImageView imageView) {
        Glide.with(mContext).load(resId).asBitmap().placeholder(placeHolderId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param file
     * @param placeHolderId
     * @param imageView
     */
    public void loadBitmap(File file, int placeHolderId, ImageView imageView) {
        Glide.with(mContext).load(file).asBitmap().placeholder(placeHolderId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param uri
     * @param placeHolderId
     * @param imageView
     */
    public void loadBitmap(Uri uri, int placeHolderId, ImageView imageView) {
        Glide.with(mContext).load(uri).asBitmap().placeholder(placeHolderId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param url
     * @param placeHolderId
     * @param errorId
     * @param imageView
     */
    public void loadBitmap(String url, int placeHolderId, int errorId, ImageView imageView) {
        Glide.with(mContext).load(url).asBitmap().placeholder(placeHolderId).error(errorId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param resId
     * @param placeHolderId
     * @param errorId
     * @param imageView
     */
    public void loadBitmap(int resId, int placeHolderId, int errorId, ImageView imageView) {
        Glide.with(mContext).load(resId).asBitmap().placeholder(placeHolderId).error(errorId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param file
     * @param placeHolderId
     * @param errorId
     * @param imageView
     */
    public void loadBitmap(File file, int placeHolderId, int errorId, ImageView imageView) {
        Glide.with(mContext).load(file).asBitmap().placeholder(placeHolderId).error(errorId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载bitmap图片
     * @param uri
     * @param placeHolderId
     * @param errorId
     * @param imageView
     */
    public void loadBitmap(Uri uri, int placeHolderId, int errorId, ImageView imageView) {
        Glide.with(mContext).load(uri).asBitmap().placeholder(placeHolderId).error(errorId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    /**
     * 加载视频
     * @param path
     * @param imageView
     */
    public void loadVideo(String path, ImageView imageView) {
        Glide.with(mContext).load( Uri.fromFile(new File(path))).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    public void clearMemory() {
        Glide.get(mContext).clearMemory();
    }

    public void clearDiskCache() {
        Glide.get(mContext).clearDiskCache();
    }

    /**
     * 唤醒请求
     */
    public void resumeRequests(){
        Glide.with(mContext).resumeRequests();
    }

    /**
     * 取消请求
     */
    public void pauseRequests(){
        Glide.with(mContext).pauseRequests();
    }
}
